//
//  DHSubscribe_BackController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/14.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHSubscribe_BackController.h"
#import "Address.h"
#import "DHContactViewController.h"

@interface DHSubscribe_BackController ()

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UIButton *addressButton;
@property (weak, nonatomic) IBOutlet UIButton *returnButton;

@property (strong, nonatomic) Address *address;

@property (strong, nonatomic) NSMutableArray *lease_ids;

@end

@implementation DHSubscribe_BackController

- (NSMutableArray *)lease_ids{
    if (!_lease_ids) {
        _lease_ids = [NSMutableArray array];
    }
    return _lease_ids;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"归还服饰";
    
    self.addressButton.layer.borderWidth = 1;
    self.addressButton.layer.borderColor = [[UIColor colorWithHexString:@"#707070"] CGColor];
    self.addressButton.layer.cornerRadius = self.addressButton.height/2;
    self.returnButton.layer.cornerRadius = self.returnButton.height/2;
    
    self.scrollView.contentSize = CGSizeMake((84 + 20) * self.arr.count, self.scrollView.height);
    [self.arr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake((84 + 20)* idx, 0, 84, self.scrollView.height)];
        [imgView dh_setImageURLString:[obj objectForKey:@"image"]];
        [self.scrollView addSubview:imgView];
        [self.lease_ids addObject:[obj objectForKey:@"lease_id"]];
    }];
    
    [self showAddress];
    
    addNObserver(@selector(changeAddress:), @"subscribe_back");
    
    //按钮点击
    [self.returnButton bk_addEventHandler:^(id sender) {
        if (!self.address) {
            [NSObject showHudTipStr:@"请添加归还地址"];
            return ;
        }else{
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            [params setObject:self.lease_ids forKey:@"lease_ids"];
            
            NSDictionary *pickup = @{@"contact":self.address.name,
                                     @"province":self.address.province,
                                     @"city":self.address.city,
                                     @"district":self.address.district,
                                     @"address":self.address.address,
                                     @"accurate":self.address.accurate,
                                     @"phone":self.address.phone
                                     };
            [params setObject:pickup forKey:@"pickup"];
            
            [[DHNetAPIClient shareDHNetAPIClient] request_order_apply_returnWithParams:params andBlock:^(id data, NSError *error) {
                [NSObject showHudTipStr:@"归还成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.addressButton bk_addEventHandler:^(id sender) {
        DHContactViewController *addressController = [[DHContactViewController alloc] init];
        addressController.address = self.address;
        addressController.isBack = YES;
        [self.navigationController pushViewController:addressController animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)dealloc
{
    removeNObserver(@"subscribe_back");
}

- (void)changeAddress:(NSNotification *)not{
    if (not){
        NSDictionary *dic = not.object;
        self.address = [dic objectForKey:@"address"];
        self.nameLabel.text = [NSString stringWithFormat:@"联系人：%@",self.address.name];
        self.phoneLabel.text = [NSString stringWithFormat:@"联系电话：%@",self.address.phone];
        self.addressLabel.text = [NSString stringWithFormat:@"地址：%@%@%@%@",self.address.province? :@"",self.address.city? :@"",self.address.district? :@"",self.address.address? :@""];
        
        NSData *addressData = [NSKeyedArchiver archivedDataWithRootObject:self.address];
        [[NSUserDefaults standardUserDefaults] setObject:addressData forKey:USER_ORDER_BUY_ADDRESS];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)showAddress{
    NSData *addressData = [[NSUserDefaults standardUserDefaults] objectForKey:USER_ORDER_BUY_ADDRESS];
    self.address = [NSKeyedUnarchiver unarchiveObjectWithData:addressData];
    if (self.address) {
        self.nameLabel.text = [NSString stringWithFormat:@"联系人：%@",self.address.name];
        self.phoneLabel.text = [NSString stringWithFormat:@"联系电话：%@",self.address.phone];
        self.addressLabel.text = [NSString stringWithFormat:@"地址:%@%@%@%@",self.address.province? :@"",self.address.city? :@"",self.address.district? :@"",self.address.address? :@""];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
